//
//  newTweet.m
//  weibo3
//
//  Created by Tian Wu on 11-4-15.
//  Copyright 2011年 nfsysu. All rights reserved.
//

#import "newTweet.h"


@implementation newTweet

@synthesize imgURL, retweeted , replyTo;



- (NSString *)windowNibName
{
	return @"newTweet";
}

- (IBAction) selectImg:(id) sender{
    
    NSOpenPanel *attachmentPanel = [NSOpenPanel openPanel];
    [attachmentPanel setAllowsMultipleSelection:false];
    [attachmentPanel setCanChooseFiles:YES];
    [attachmentPanel setCanChooseDirectories:FALSE];
    if ( [attachmentPanel runModalForDirectory:nil file:nil] == NSOKButton )
    {
        // Get an array containing the full filenames of all
        // files and directories selected.
        NSArray* files = [attachmentPanel filenames];
        [path setTitleWithMnemonic:[files objectAtIndex:0]];
        
        self.imgURL = [NSString stringWithFormat:@"file://%@",[files objectAtIndex:0]];
    }
    
}

- (BOOL)edit:(Status *)startingValues from:(id)sender
{
        
	[NSApp beginSheet:[self window] modalForWindow:[sender window] modalDelegate:nil didEndSelector:nil contextInfo:nil];
    if (startingValues != nil) {
        
        if([[startingValues retweeted_status] text] != nil){
            
            NSString *temp = [NSString stringWithFormat:@" //@%@: %@",[[startingValues user] name],[startingValues text]];
            [text setString:temp];
            [text setSelectedRange:NSMakeRange(0, 0)];
            self.retweeted = [startingValues retweeted_status];
            
        }
        else{
            
            self.retweeted = startingValues;
            
        }
    }
	[NSApp runModalForWindow:[self window]];
    
	// sheet is up here...
    
	[NSApp endSheet:[self window]];
	[[self window] orderOut:self];
    
    return YES;
}

- (IBAction) replyComment:(id) sender{
    
    NSString *status = [text string];
    
    if ([status length] > 140) {
        NSAlert *alert = [NSAlert alertWithMessageText:@"字数太多了" defaultButton:@"好的" alternateButton:nil otherButton:nil informativeTextWithFormat:@"最多可以输入140字，而您已经输入了%d字噢！",[[text string] length]];
        [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:NULL];
    }
    else if([status length] < 1){
        NSAlert *alert = [NSAlert alertWithMessageText:@"请输入文字" defaultButton:@"好的" alternateButton:nil otherButton:nil informativeTextWithFormat:@"请在白色区域中输入您想要发送的微博！"];
        [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:NULL];
    }
    else{
        
        
        
        
        NSLog(@"status:%@",status);
        
        [_rootViewController statusReply:[[self.replyTo status] aid] commentId:[self.replyTo aid] comment:status];
        
        [NSApp stopModal];
        [text setString:@""];
        [path setTitleWithMnemonic:@" "];

    }
    
    
    
}

- (BOOL)didReplyToComment:(WComment *)startingValues from:(id)sender{
    [NSApp beginSheet:[self window] modalForWindow:[[NSApplication sharedApplication] mainWindow] modalDelegate:nil didEndSelector:nil contextInfo:nil];
    
    self.replyTo  = startingValues;
    [sendButton setAction:@selector(replyComment:)];
    
	[NSApp runModalForWindow:[self window]];
    
	// sheet is up here...
    
	[NSApp endSheet:[self window]];
	[[self window] orderOut:self];
    
    return YES;
    
}




- (IBAction) sendIt:(id) sender{
    
    NSString *status = [text string];
    
    if ([status length] > 140) {
        NSAlert *alert = [NSAlert alertWithMessageText:@"字数太多了" defaultButton:@"好的" alternateButton:nil otherButton:nil informativeTextWithFormat:@"最多可以输入140字，而您已经输入了%d字噢！",[[text string] length]];
        [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:NULL];
    }
    else if([status length] < 1){
        NSAlert *alert = [NSAlert alertWithMessageText:@"请输入文字" defaultButton:@"好的" alternateButton:nil otherButton:nil informativeTextWithFormat:@"请在白色区域中输入您想要发送的微博！"];
        [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:NULL];
    }
    else{
        
        
        
        
        
        if ([self.retweeted text] != nil) {
            [_rootViewController statusRepost:[self.retweeted aid] status:status isComment:0];
        }
        else{
            if (self.imgURL == NULL) {
                [_rootViewController statusUpdate:status inReplyToStatusId:nilReplyId latitude:nilLatitude longitude:nilLongitude];
            }
            else{
                
                [_rootViewController statusUpload:status pic:self.imgURL latitude:nilLatitude longitude:nilLongitude];
                self.imgURL = NULL;
            }
        }
        [text setString:@""];
        
        [NSApp stopModal];
        
        [path setTitleWithMnemonic:@" "];
        
    }
    
    
    
    
}

- (IBAction) cancel:(id) sender{
    [NSApp stopModal];
}

- (void) handleInsertEmotion:(NSNotification *)emotion{
    
    [text insertText:[emotion object]];
    
}


- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
    }
    
    return self;
}


- (void)dealloc
{
    [super dealloc];
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    emotionView = [[emotionViewController alloc] initWithNibName:@"emotionViewController" bundle:nil];
    NSMenuItem *custMenuItem = [[emotionButton menu] itemAtIndex:0];
	[custMenuItem setView:[emotionView view]];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleInsertEmotion:)
                                                 name:@"HandleCollectionItem"
                                               object:nil];
    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
    [attributes setObject: [NSFont fontWithName:@"Lucida Grande" size:13] forKey:
     NSFontAttributeName];
    
    [text setTypingAttributes:attributes];

}

@end
